import { loc } from 'local/index';

const getClientH = () => document.body.clientHeight;

const is_empty = (data) => {
    if (!data) {
        return true;
    }

    return data.length <= 0;
};

const obj_empty = (data) => {
    if (!data) {
        return true;
    }

    return Object.keys(data).length <= 0;
};

const obj_str = (v, k) => {
    if (obj_empty(v)) {
        return "";
    }

    const r = v[k];
    if (r) {
        return "" + r;
    }
    else {
        if (`${r}` === "false") {
            return "false";
        }

        if (!isNaN(r)) {
            return "" + r;
        }
    }

    return "";
};

const obj_bool = (v, k) => {
    if (obj_empty(v)) {
        return false;
    }

    const val = v[k];
    if (val === "true") {
        return true;
    }
    else if (val === "false") {
        return false;
    }

    if (val) {
        return val;
    }

    return false;
};

const for_obj = (obj, fn) => {
    if (!obj || !fn) {
        return;
    }

    Object.entries(obj).forEach(([k, v]) => fn(k, v));
};

const obj_int = (v, k) => {
    if (!v) {
        return 0;
    }

    let n = parseInt(v[k], 10);
    if (isNaN(n)) {
        return 0;
    }

    return n;
};

const cut_text = (v, len=4) => {
    if (!v) {
        return "";
    }

    if (v.length > len) {
        let r = v.slice(0, len);
        r += "...";
        return r;
    }

    return v;
};

const d_sort_num = (r, k) => {
    r.sort((a, b) => parseInt(a[k], 10) - parseInt(b[k], 10));
};

const d_sort = (r, k, rev=false) => {
    if (is_empty(r)) {
        return;
    }

    if (rev) {
        r.sort((a, b) => {
            if (a[k] > b[k]) {
                return -1;
            }

            if (a[k] < b[k]) {
                return 1;
            }

            return 0;
        });

        return;
    }

    r.sort((a, b) => {
        if (a[k] > b[k]) {
            return 1;
        }

        if (a[k] < b[k]) {
            return -1;
        }

        return 0;
    });
};

const sleep_base = (ms) => new Promise(r => setTimeout(r, ms));

const sleep = (tm, hcmd) => sleep_base(tm).then(() => hcmd());

const no_del = (m) => {
    if (!window.confirm(m)) {
        return true;
    }

    return false;
};

const ck_empty = (v) => v ? null : "不能为空";

const ck_float = (v) => {
    const val = "" + v;
    if (!val) {
        return "请输入数字";
    }

    const f = parseFloat(val);
    if (isNaN(f)) {
        return "请输入数字";
    }

    return null;
}

const obj_float = (v, k) => {
    if (!v) {
        return 0;
    }

    const n = parseFloat(v[k]);
    if (isNaN(n)) {
        return 0;
    }

    return n;
};

const box_tp = (t) => {
    if (t === "outer") {
        return "外圈";
    } else if (t === "inner") {
        return "内圈";
    } else if (t === "pair") {
        return "配对";
    }

    return null;
};

const show_mt = (v) => {
    if (v === "G") {
        return "钢球";
    } else if (v === "T") {
        return "陶瓷球";
    }

    return v;
};

const wrong_reg = (data, reg, info) => {
    if (reg.test(data)) {
        return null;
    }

    return `${data}: ${info}`;
};

const ascii_print = (data) => {
    return wrong_reg(data, /^[\x20-\x7E]*$/, loc("l_505"));
};

const ck_ascii = (v) => {
    if (!v) {
        return "不能为空";
    }

    const tmp = v.trim();
    if (!tmp) {
        return "不能为空";
    }

    let r = ascii_print(tmp);
    if (r) {
        return r;
    }

    return null;
};

const ck_int = (v, info) => {
    const val = "" + v;
    if (!val) {
        return "请输入数字";
    }

    const f = parseInt(val, 10);
    if (isNaN(f)) {
        return "请输入数字";
    }

    return null;
}

const obj_str_trim = (v, k) => {
    let r = obj_str(v, k);
    return r.trim();
};

const obj_date = (v, k) => {
    if (obj_empty(v)) {
        return new Date();
    }

    if (v[k]) {
        return v[k];
    }

    return new Date();
};

const ck_date = (v) => {
    if (!v) {
        return "请输入正确日期";
    }

    return null;
};

const ck_date_range = (start, end) => {
    let r = ck_date(end);
    if (r) {
        return r;
    }

    r = ck_date(start);
    if (r) {
        return r;
    }

    if (start > end) {
        return "开始时间要大于结束时间";
    }

    return null;
};

export {ck_int, obj_float, ck_float, show_mt, box_tp, ck_empty, no_del, sleep, d_sort, d_sort_num, getClientH, obj_int, obj_empty, for_obj, obj_bool,
    ck_date, cut_text, obj_str, obj_str_trim, ck_ascii, is_empty, obj_date, ck_date_range};
